<?php
class Welcome extends CI_Controller {
	public function __construct() {
		parent::__construct();
		$this->load->helper('url');
		$this->load->library('session');

		if (!$this->session->islogin) {
			$currentUrl = current_url();
			$currentUrl = explode('Welcome/', $currentUrl);

			if (empty($currentUrl[1]) || $currentUrl[1] == 'register' || $currentUrl[1] == 'loginAction') {
				return;
			} else {
				$arr = [];
				$arr['status'] = -1;
				$arr['message'] = '未登录';
				redirect(site_url() . '/Welcome/register');
			}
		}
	}

	//注册
	public function enroll() {
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$email = $this->input->post('email');

		$data = array(
			'username' => $username,
			'password' => sha1(md5($password . 'adserf')),
			'email' => $email,
		);
		$result = $this->db->insert('admin', $data);
		redirect('/Welcome/index');
	}

	//注册页面
	public function sign() {
		$this->load->view('sign');
	}

	//登录
	public function loginAction() {
		$username = $this->input->post('username');
		$password = $this->input->post('password');

		$where = [];
		$where['username'] = $username;
		$where['password'] = sha1(md5($password . 'adserf'));
		$result = $this->db->select('*')->from('admin')->where($where)->get()->result_array();
		
		if (count($result) != 0) {

			$this->session->username = $result[0]['username'];
			$this->session->islogin = 1;
			
			$arr = [];
			$arr['status'] = 1;
			$arr['message'] = '登录成功';
			$this->ajaxResult($arr);
		} else {
			
			$arr = [];
			$arr['status'] = 0;
			$arr['message'] = '账号或密码错误';
			$this->ajaxResult($arr);
		}
	}

	//注销
	public function exit() {
		session_destroy();
		$arr = [];
		$arr['status'] = 1;
		$arr['message'] = '注销成功';
		$this->ajaxResult($arr);
	}

	//首页
	// public function index() {
	// 	$this->load->view('public/header');
	// 	$this->load->view('public/left');
	// 	$this->load->view('index');
	// }

	//添加商品的页面
	public function index() {
		$link = $this->input->post('link');
		$data = [];
		$result = $this->db->select('*')->from('title')->get()->result_array();
		$data['result'] = $result;
		$attent = $this->db->select('*')->from('norms')->get()->result_array();
		$data['attent'] = $attent;

		$this->load->view('public/header');
		$this->load->view('public/left');
		$this->load->view('add',$data);
	}

	//添加商品
	public function ankh() {
		$picture = $this->input->post('picture');
		$price = $this->input->post('price');
		$name = $this->input->post('name');
		$standard = $this->input->post('standard');
		$content = $this->input->post('content');
		$link = $this->input->post('link');

		$data = array(
			'image_url' => $picture,
			'price' => $price,
			'name' => $name,
			'standard' => $standard,
			'content' => $content,
			'link' => $link,
		);
		$this->db->insert('add', $data);
		redirect('/Welcome/index');
	}

	//查看分类
	public function classification() {
		$result = $this->db->select('count(1)')->from('title')->get()->result_array();
		$resultNum = $result[0]['count(1)'];
		$resultNum = $resultNum / 5;
		$data = [];
		$data['result'] = $result;
		$data['wert'] = $resultNum;
		$pace = $this->input->get('page');
		if (!isset($pace)) {
			$pace = 1;
		}

		$aa = ($pace - 1) * 5;
		$attent = $this->db->select('*')->from('title')->limit(5, $aa)->get()->result_array();
		$data['result'] = $attent;
		$this->load->view('public/header');
		$this->load->view('public/left');
		$this->load->view('classification', $data);
	}

	//添加商品分类
	public function user() {
		$title = $this->input->post('title');
		$picture = $this->input->post('picture');
		$price = $this->input->post('price');
		$rivate = $this->input->post('rivate');
		$data = array(
			'title' => $title,
			'image' => $picture,
			'size' => $price,
			'appearance' => $rivate,
		);
		$this->db->insert('title', $data);
		redirect('/Welcome/classification');
	}

	//添加用户信息
	public function userinfo() {
		$this->load->view('public/header');
		$this->load->view('public/left');
		$this->load->view('userinfo');
	}

	//将用户信息中的图片添加到指定文件夹
	public function upankh() {
		$filename = rand(0, 10000);

		$filename .= '.' . explode('.', $_FILES['youFile']['name'])[1];
		$filePath = base_url() . 'upankh/' . $filename;
		$config['upload_path'] = './upankh/';
		$config['file_name'] = $filename;
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
		$this->load->library('upload', $config);
		if ($this->upload->do_upload('youFile') == true) {
			$data = [];
			$data['status'] = 1;
			$data['message'] = '上传成功';
			$data['filePath'] = $filePath;
			$this->ajaxResult($data);
		}
	}

	//将用户信息添加到数据库
	public function zio() {
		$head = $this->input->post('head');
		$name = $this->input->post('name');
		$stature = $this->input->post('stature');
		$weight = $this->input->post('weight');
		$location = $this->input->post('location');
		$occupation = $this->input->post('occupation');
		$image1 = $this->input->post('image1');
		$image2 = $this->input->post('image2');
		$image3 = $this->input->post('image3');

		$data = array(
			'head' => $head,
			'name' => $name,
			'stature' => $stature,
			'weight' => $weight,
			'location' => $location,
			'occupation' => $occupation,
			'image1' => $image1,
			'image2' => $image2,
			'image3' => $image3,
		);
		$this->db->insert('user', $data);
		redirect('/Welcome/userinfo');
	}

	//商品信息
	public function commodity() {
		$result = $this->db->select('count(1)')->from('add')->get()->result_array();
		$resultNum = $result[0]['count(1)'];
		$resultNum = $resultNum / 5;
		$data = [];
		$data['result'] = $result;
		$data['wert'] = $resultNum;
		$pace = $this->input->get('page');
		if (!isset($pace)) {
			$pace = 1;
		}

		$aa = ($pace - 1) * 5;
		$attent = $this->db->select('*')->from('add')->limit(5, $aa)->get()->result_array();
		$data['result'] = $attent;
		$this->load->view('public/header');
		$this->load->view('public/left');
		$this->load->view('list', $data);
	}

	//登录
	public function register() {
		$this->load->view('login');
	}

	//添加商品图片
	public function uploadFiles() {
		$filename = time() . rand(0, 10000);

		$filename .= '.' . explode('.', $_FILES['youFile']['name'])[1];
		$filePath = base_url() . 'upload/' . $filename;
		$config['upload_path'] = './upload/';
		$config['file_name'] = $filename;
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
		$this->load->library('upload', $config);
		if ($this->upload->do_upload('youFile') == true) {
			$data = [];
			$data['status'] = 1;
			$data['message'] = '上传成功';
			$data['filePath'] = $filePath;
			$this->ajaxResult($data);
		}
	}

	//添加图片
	public function uploadFile() {
		$filename = time() . rand(0, 10000);

		$filename .= '.' . explode('.', $_FILES['youFile']['name'])[1];
		$filePath = base_url() . 'uploads/' . $filename;
		$config['upload_path'] = './uploads/';
		$config['file_name'] = $filename;
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
		$this->load->library('upload', $config);
		if ($this->upload->do_upload('youFile') == true) {
			$data = [];
			$data['status'] = 1;
			$data['message'] = '上传成功';
			$data['filePath'] = $filePath;
			$this->ajaxResult($data);
		}
	}

	//对变量进行json编码
	private function ajaxResult($data) {
		echo json_encode($data);
		die;
	}

	//添加商品规格页面
	public function norms() {
		$result = $this->db->select('*')->from('title')->get()->result_array();
		$data = [];
		$data['result'] = $result;
		$this->load->view('public/header');
		$this->load->view('public/left');
		$this->load->view('norms',$data);
	}

	//添加商品规格
	public function addNorms() {
		$link = $this->input->post('link');
		$picture = $this->input->post('picture');
		$size = $this->input->post('size');

		$data = array(
			'title' => $link,
			'norms1' => $picture,
			'norms2' => $size,
		);

		$this->db->insert('norms',$data);
		redirect('/Welcome/norms');
	}

	//分类参数设置页面
	public function testa()	{
		$result = $this->db->select('*')->from('atch')->get()->result_array();
		$data = [];
		$data['result'] = $result;
		$this->load->view('testa', $data);
	}

	//删除分类参数
	public function admin() {
		$id = $this->input->post('id');
		$data = [];
		$data['id'] = $id;
		$this->db->where($data);
		$this->db->delete('atch');
		redirect('/Welcome/testa');
	}

	//添加分类参数
	public function gentout() {
		$title = $this->input->post('title');
		$rivate = $this->input->post('rivate');
		$data = array(
			'title' => $title,
			'rivate' => $rivate,
		);

		$this->db->insert('atch',$data);
		redirect('/Welcome/testa');
	}
}